import { governmentEnterpriseLogin, personalLogin, emailRegister, getInfo, logout } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  user_id: 0,
  user_email: '',
  user_phone_number: '',
  user_nickname: '',
  user_avatar: '',
  user_roles: [],
  user_group: '',
  user_permissions: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_ID: (state, user_id) => {
    state.user_id = user_id
  },
  SET_USER_EMAIL: (state, user_email) => {
    state.user_email = user_email
  },
  SET_USER_PHONE_NUMBER: (state, user_phone_number) => {
    state.user_phone_number = user_phone_number
  },
  SET_USER_NICKNAME: (state, user_nickname) => {
    state.user_nickname = user_nickname
  },
  SET_USER_AVATAR: (state, user_avatar) => {
    state.user_avatar = user_avatar
  },
  SET_USER_ROLES: (state, user_roles) => {
    state.user_roles = user_roles
  },
  SET_USER_GROUP: (state, user_group) => {
    state.user_group = user_group
  },
  SET_USER_PERMISSIONS: (state, user_permissions) => {
    state.user_permissions = user_permissions
  }
}

const actions = {
  // 政企用户登录
  governmentEnterpriseLogin({ commit }, userInfo) {
    const { email, password } = userInfo
    return new Promise((resolve, reject) => {
      governmentEnterpriseLogin(email.trim(), password).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 个人用户登录
  personalLogin({ commit }, userInfo) {
    const { phoneNumber, verificationCode } = userInfo
    return new Promise((resolve, reject) => {
      personalLogin(phoneNumber, verificationCode).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user emailRegister
  emailRegister({ commit }, userInfo) {
    const { email, password } = userInfo
    return new Promise((resolve, reject) => {
      emailRegister(email.trim(), password).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (!data) {
          reject('验证失败，请重新登录！')
        }

        const { user_id, user_email, user_phone_number, user_nickname, user_avatar, user_roles, user_group, user_permissions } = data

        // roles must be a non-empty array
        if (!user_roles || user_roles.length <= 0) {
          reject('获取用户详细信息失败: 角色必须是非空数组！')
        }
        commit('SET_USER_ID', user_id)
        commit('SET_USER_EMAIL', user_email)
        commit('SET_USER_PHONE_NUMBER', user_phone_number)
        commit('SET_USER_NICKNAME', user_nickname)
        commit('SET_USER_AVATAR', user_avatar)
        commit('SET_USER_ROLES', user_roles)
        commit('SET_USER_GROUP', user_group)
        commit('SET_USER_PERMISSIONS', user_permissions)

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        commit('SET_TOKEN', '')
        commit('SET_USER_ROLES', [])
        commit('SET_USER_GROUP', '')
        commit('SET_USER_PERMISSIONS', [])
        removeToken()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
